package com.ndp.fb.rdb.db;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.StatementCallback;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 进行主备，如果不能确定的话，直接抛给主库执行，备库可能没有update权限
 * <p/>
 * Created by infi.he on 2015/6/2.
 */
@Component
public class JdbcTemplateMixed implements JdbcOperations {

    private static String SELECT_LOWER = "select";
    private static String SELECT_UPPER = "SELECT";
    @Autowired
    @Qualifier("jdbcWriteTemplate")
    private JdbcTemplate jdbcWriteTemplate;

//    @Autowired
//    @Qualifier("jdbcReadTemplate")
//    private JdbcTemplate jdbcReadTemplate;

    public JdbcTemplate getJdbcWriteTemplate() {
        return jdbcWriteTemplate;
    }


    public JdbcTemplate getJdbcReadTemplate() {
        return jdbcWriteTemplate;
    }


    @Override
    public <T> T execute(ConnectionCallback<T> connectionCallback) throws DataAccessException {
        return jdbcWriteTemplate.execute(connectionCallback);
    }

    @Override
    public <T> T execute(StatementCallback<T> statementCallback) throws DataAccessException {
        return jdbcWriteTemplate.execute(statementCallback);
    }

    @Override
    public void execute(String s) throws DataAccessException {
        jdbcWriteTemplate.execute(s);
    }

    @Override
    public <T> T query(String s, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException {
        return jdbcWriteTemplate.query(s, resultSetExtractor);
    }

    @Override
    public void query(String s, RowCallbackHandler rowCallbackHandler) throws DataAccessException {
        jdbcWriteTemplate.query(s, rowCallbackHandler);
    }

    @Override
    public <T> List<T> query(String s, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcWriteTemplate.query(s, rowMapper);
    }

    @Override
    public <T> T queryForObject(String s, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcWriteTemplate.queryForObject(s, rowMapper);
    }

    @Override
    public <T> T queryForObject(String s, Class<T> aClass) throws DataAccessException {
        return jdbcWriteTemplate.queryForObject(s, aClass);
    }

    @Override
    public Map<String, Object> queryForMap(String s) throws DataAccessException {
        return jdbcWriteTemplate.queryForMap(s);
    }

    public long queryForLong(String s) throws DataAccessException {
        Number number = queryForObject(s, Long.class);
        return (number != null ? number.longValue() : 0);
    }

    public int queryForInt(String s) throws DataAccessException {
        Number number = queryForObject(s, Integer.class);
        return (number != null ? number.intValue() : 0);
    }

    @Override
    public <T> List<T> queryForList(String s, Class<T> aClass) throws DataAccessException {
        return jdbcWriteTemplate.queryForList(s, aClass);
    }

    @Override
    public List<Map<String, Object>> queryForList(String s) throws DataAccessException {
        return jdbcWriteTemplate.queryForList(s);
    }

    @Override
    public SqlRowSet queryForRowSet(String s) throws DataAccessException {
        return jdbcWriteTemplate.queryForRowSet(s);
    }

    @Override
    public int update(String s) throws DataAccessException {

        return jdbcWriteTemplate.update(s);

    }

    @Override
    public int[] batchUpdate(String... strings) throws DataAccessException {
        return jdbcWriteTemplate.batchUpdate(strings);
    }

    @Override
    public <T> T execute(PreparedStatementCreator preparedStatementCreator, PreparedStatementCallback<T> preparedStatementCallback) throws DataAccessException {
        return jdbcWriteTemplate.execute(preparedStatementCreator, preparedStatementCallback);
    }

    @Override
    public <T> T execute(String s, PreparedStatementCallback<T> preparedStatementCallback) throws DataAccessException {

        return jdbcWriteTemplate.execute(s, preparedStatementCallback);

    }

    @Override
    public <T> T query(PreparedStatementCreator preparedStatementCreator, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException {
        return jdbcWriteTemplate.query(preparedStatementCreator, resultSetExtractor);
    }

    @Override
    public <T> T query(String s, PreparedStatementSetter preparedStatementSetter, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException {
        return jdbcWriteTemplate.query(s, preparedStatementSetter, resultSetExtractor);
    }

    @Override
    public <T> T query(String s, Object[] objects, int[] ints, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException {
        return jdbcWriteTemplate.query(s, objects, ints, resultSetExtractor);
    }

    @Override
    public <T> T query(String s, Object[] objects, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException {
        return jdbcWriteTemplate.query(s, objects, resultSetExtractor);
    }

    @Override
    public <T> T query(String s, ResultSetExtractor<T> resultSetExtractor, Object... objects) throws DataAccessException {
        return jdbcWriteTemplate.query(s, resultSetExtractor, objects);
    }

    @Override
    public void query(PreparedStatementCreator preparedStatementCreator, RowCallbackHandler rowCallbackHandler) throws DataAccessException {
        jdbcWriteTemplate.query(preparedStatementCreator, rowCallbackHandler);
    }

    @Override
    public void query(String s, PreparedStatementSetter preparedStatementSetter, RowCallbackHandler rowCallbackHandler) throws DataAccessException {
        jdbcWriteTemplate.query(s, preparedStatementSetter, rowCallbackHandler);

    }

    @Override
    public void query(String s, Object[] objects, int[] ints, RowCallbackHandler rowCallbackHandler) throws DataAccessException {
        jdbcWriteTemplate.query(s, objects, ints, rowCallbackHandler);

    }

    @Override
    public void query(String s, Object[] objects, RowCallbackHandler rowCallbackHandler) throws DataAccessException {
        jdbcWriteTemplate.query(s, objects, rowCallbackHandler);

    }

    @Override
    public void query(String s, RowCallbackHandler rowCallbackHandler, Object... objects) throws DataAccessException {
        jdbcWriteTemplate.query(s, rowCallbackHandler, objects);
    }

    @Override
    public <T> List<T> query(PreparedStatementCreator preparedStatementCreator, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcWriteTemplate.query(preparedStatementCreator, rowMapper);
    }

    @Override
    public <T> List<T> query(String s, PreparedStatementSetter preparedStatementSetter, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcWriteTemplate.query(s, preparedStatementSetter, rowMapper);
    }

    @Override
    public <T> List<T> query(String s, Object[] objects, int[] ints, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcWriteTemplate.query(s, objects, ints, rowMapper);
    }

    @Override
    public <T> List<T> query(String s, Object[] objects, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcWriteTemplate.query(s, objects, rowMapper);
    }

    @Override
    public <T> List<T> query(String s, RowMapper<T> rowMapper, Object... objects) throws DataAccessException {
        return jdbcWriteTemplate.query(s, rowMapper, objects);
    }

    @Override
    public <T> T queryForObject(String s, Object[] objects, int[] ints, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcWriteTemplate.queryForObject(s, rowMapper, objects);
    }

    @Override
    public <T> T queryForObject(String s, Object[] objects, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcWriteTemplate.queryForObject(s, objects, rowMapper);
    }

    @Override
    public <T> T queryForObject(String s, RowMapper<T> rowMapper, Object... objects) throws DataAccessException {
        return jdbcWriteTemplate.queryForObject(s, rowMapper, objects);
    }

    @Override
    public <T> T queryForObject(String s, Object[] objects, int[] ints, Class<T> aClass) throws DataAccessException {
        return jdbcWriteTemplate.queryForObject(s, objects, ints, aClass);
    }

    @Override
    public <T> T queryForObject(String s, Object[] objects, Class<T> aClass) throws DataAccessException {
        return jdbcWriteTemplate.queryForObject(s, objects, aClass);
    }

    @Override
    public <T> T queryForObject(String s, Class<T> aClass, Object... objects) throws DataAccessException {
        return jdbcWriteTemplate.queryForObject(s, aClass, objects);
    }

    @Override
    public Map<String, Object> queryForMap(String s, Object[] objects, int[] ints) throws DataAccessException {
        return jdbcWriteTemplate.queryForMap(s, objects, ints);
    }

    @Override
    public Map<String, Object> queryForMap(String s, Object... objects) throws DataAccessException {
        return jdbcWriteTemplate.queryForMap(s, objects);
    }


    public int queryForInt(String s, Object... objects) throws DataAccessException {
        Number number = queryForObject(s, objects, Integer.class);
        return (number != null ? number.intValue() : 0);
    }

    @Override
    public <T> List<T> queryForList(String s, Object[] objects, int[] ints, Class<T> aClass) throws DataAccessException {
        return jdbcWriteTemplate.queryForList(s, objects, ints, aClass);
    }

    @Override
    public <T> List<T> queryForList(String s, Object[] objects, Class<T> aClass) throws DataAccessException {
        return jdbcWriteTemplate.queryForList(s, objects, aClass);
    }

    @Override
    public <T> List<T> queryForList(String s, Class<T> aClass, Object... objects) throws DataAccessException {
        return jdbcWriteTemplate.queryForList(s, aClass, objects);
    }

    @Override
    public List<Map<String, Object>> queryForList(String s, Object[] objects, int[] ints) throws DataAccessException {
        return jdbcWriteTemplate.queryForList(s, objects, ints);
    }

    @Override
    public List<Map<String, Object>> queryForList(String s, Object... objects) throws DataAccessException {
        return jdbcWriteTemplate.queryForList(s, objects);
    }

    @Override
    public SqlRowSet queryForRowSet(String s, Object[] objects, int[] ints) throws DataAccessException {
        return jdbcWriteTemplate.queryForRowSet(s, objects, ints);
    }

    @Override
    public SqlRowSet queryForRowSet(String s, Object... objects) throws DataAccessException {
        return jdbcWriteTemplate.queryForRowSet(s, objects);
    }

    @Override
    public int update(PreparedStatementCreator preparedStatementCreator) throws DataAccessException {
        return jdbcWriteTemplate.update(preparedStatementCreator);
    }

    @Override
    public int update(PreparedStatementCreator preparedStatementCreator, KeyHolder keyHolder) throws DataAccessException {
        return jdbcWriteTemplate.update(preparedStatementCreator, keyHolder);
    }

    @Override
    public int update(String s, PreparedStatementSetter preparedStatementSetter) throws DataAccessException {
        return jdbcWriteTemplate.update(s, preparedStatementSetter);
    }

    @Override
    public int update(String s, Object[] objects, int[] ints) throws DataAccessException {
        return jdbcWriteTemplate.update(s, objects, ints);
    }

    @Override
    public int update(String s, Object... objects) throws DataAccessException {
        return jdbcWriteTemplate.update(s, objects);
    }

    @Override
    public int[] batchUpdate(String s, BatchPreparedStatementSetter batchPreparedStatementSetter) throws DataAccessException {
        return jdbcWriteTemplate.batchUpdate(s, batchPreparedStatementSetter);
    }

    @Override
    public int[] batchUpdate(String s, List<Object[]> list) throws DataAccessException {
        return jdbcWriteTemplate.batchUpdate(s, list);
    }

    @Override
    public int[] batchUpdate(String s, List<Object[]> list, int[] ints) throws DataAccessException {
        return jdbcWriteTemplate.batchUpdate(s, list, ints);
    }

    @Override
    public <T> int[][] batchUpdate(String s, Collection<T> collection, int i, ParameterizedPreparedStatementSetter<T> parameterizedPreparedStatementSetter) throws DataAccessException {
        return jdbcWriteTemplate.batchUpdate(s, collection, i, parameterizedPreparedStatementSetter);
    }

    @Override
    public <T> T execute(CallableStatementCreator callableStatementCreator, CallableStatementCallback<T> callableStatementCallback) throws DataAccessException {
        return jdbcWriteTemplate.execute(callableStatementCreator, callableStatementCallback);
    }

    @Override
    public <T> T execute(String s, CallableStatementCallback<T> callableStatementCallback) throws DataAccessException {
        return jdbcWriteTemplate.execute(s, callableStatementCallback);
    }

    @Override
    public Map<String, Object> call(CallableStatementCreator callableStatementCreator, List<SqlParameter> list) throws DataAccessException {
        return jdbcWriteTemplate.call(callableStatementCreator, list);
    }
}
